{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python中的字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What Is a Dictionary?\n",
    "什么是字典\n",
    "Mutable unordered set…\n",
    "可变无序集......"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mutable可变：意味着它里面的对象可以像列表一样添加、修改、删除"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## unordered无序：意味着字典里面的对象排列顺序改变，不改变字典本身"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## set集：字典是一个集合，类似于列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## key:value pairs键值对\n",
    "每一个键和值之间用冒号( : )隔开，而每一个item之间用逗号隔开，整个字典放在大括号里面。一个没有任何item的空字典表示为两个大括号，例如 { }\n",
    "注意：键是独一无二的，但是值不一定。键必须是不可变的数据类型，如字符串，数字或元组；值可以是任意的数据类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典与列表、元组的区别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表List：用符号[]表示列表，中间的元素可以是任何类型，用逗号分隔。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List=[1,2,3,4,5,6]\n",
    "type(List)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组Tuple：用符号()表示元组，元组是和列表相似的数据结构，但是它一旦初始化就不能更改，速度比list快。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Tuple=(1,2,3,4,5,6)\n",
    "type(Tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一、创建字典的两种方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.第一种方式：花括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'iPhone': 2007, 'iPhone 3G': 2008, 'iPhone 3GS': 2009, 'iPhone 4': 2010, 'iPhone 4S': 2011, 'iPhone 5': 2012, 'iPhone 5S': 2013, 'iPhone 6': 2014, 'iPhone 6S': 2015, 'iPhone 7': 2016, 'iPhone 8': 2017}\n"
     ]
    }
   ],
   "source": [
    "iPhone = {\n",
    "          \"iPhone\" : 2007,\n",
    "          \"iPhone 3G\" : 2008,\n",
    "          \"iPhone 3GS\" : 2009,\n",
    "          \"iPhone 4\" : 2010,\n",
    "          \"iPhone 4S\" : 2011,\n",
    "          \"iPhone 5\" : 2012,\n",
    "          \"iPhone 5S\" : 2013,\n",
    "          \"iPhone 6\" : 2014,\n",
    "          \"iPhone 6S\" : 2015,\n",
    "          \"iPhone 7\" : 2016,\n",
    "          \"iPhone 8\" : 2017,        \n",
    "    }\n",
    "print(iPhone)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.第二种方式：dict（内建函数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'iPad': 2010, 'iPad_2': 2011, 'ipad_3': 2012, 'iPad_mini_1': 2012, 'iPad_Air_1': 2013, 'iPad_mini_2': 2013, 'iPad_Air_2': 2014, 'iPad_mini_3': 2014, 'iPad_pro_12_9': 2015, 'iPad_mini_4': 2015, 'iPad_pro9_7': 2016, 'iPad_2017': 2017, 'iPad_pro_10_5': 2017, 'iPad_pro_2_12_9': 2017, 'iPad_2018': 2018, 'iPad_pro_3_12_9': 2018, 'iPad_pro_3_11': 2018, 'iPad_Air_3': 2019, 'iPad_mini_5': 2019}\n"
     ]
    }
   ],
   "source": [
    "iPad = dict(\n",
    "          iPad = 2010,\n",
    "          iPad_2 = 2011,\n",
    "          ipad_3 = 2012,\n",
    "          iPad_mini_1 = 2012,\n",
    "          iPad_Air_1 = 2013,\n",
    "          iPad_mini_2 = 2013,\n",
    "          iPad_Air_2 = 2014,\n",
    "          iPad_mini_3 = 2014,\n",
    "          iPad_pro_12_9 = 2015,\n",
    "          iPad_mini_4 = 2015,\n",
    "          iPad_pro9_7 = 2016,\n",
    "          iPad_2017 = 2017,\n",
    "          iPad_pro_10_5 = 2017,\n",
    "          iPad_pro_2_12_9 = 2017,\n",
    "          iPad_2018 = 2018,\n",
    "          iPad_pro_3_12_9 = 2018,\n",
    "          iPad_pro_3_11 = 2018,\n",
    "          iPad_Air_3 = 2019,\n",
    "          iPad_mini_5 = 2019\n",
    "          )\n",
    "print(iPad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'iPad': 2010,\n",
       " 'iPad_2': 2011,\n",
       " 'ipad_3': 2012,\n",
       " 'iPad_mini_1': 2012,\n",
       " 'iPad_Air_1': 2013,\n",
       " 'iPad_mini_2': 2013,\n",
       " 'iPad_Air_2': 2014,\n",
       " 'iPad_mini_3': 2014,\n",
       " 'iPad_pro_12_9': 2015,\n",
       " 'iPad_mini_4': 2015,\n",
       " 'iPad_pro9_7': 2016,\n",
       " 'iPad_2017': 2017,\n",
       " 'iPad_pro_10_5': 2017,\n",
       " 'iPad_pro_2_12_9': 2017,\n",
       " 'iPad_2018': 2018,\n",
       " 'iPad_pro_3_12_9': 2018,\n",
       " 'iPad_pro_3_11': 2018,\n",
       " 'iPad_Air_3': 2019,\n",
       " 'iPad_mini_5': 2019}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二、对字典进行操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.获取键值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2017"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone['iPhone 8']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2017"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone[\"iPhone 8\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.添加键值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 直接添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'iPhone': 2007,\n",
       " 'iPhone 3G': 2008,\n",
       " 'iPhone 3GS': 2009,\n",
       " 'iPhone 4': 2010,\n",
       " 'iPhone 4S': 2011,\n",
       " 'iPhone 5': 2012,\n",
       " 'iPhone 5S': 2013,\n",
       " 'iPhone 6': 2014,\n",
       " 'iPhone 6S': 2015,\n",
       " 'iPhone 7': 2016,\n",
       " 'iPhone 8': 2017,\n",
       " 'iPhone X': 2017}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone[\"iPhone X\"] = 2017\n",
    "iPhone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 函数update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'iPhone': 2007,\n",
       " 'iPhone 3G': 2008,\n",
       " 'iPhone 3GS': 2009,\n",
       " 'iPhone 4': 2010,\n",
       " 'iPhone 4S': 2011,\n",
       " 'iPhone 5': 2012,\n",
       " 'iPhone 5S': 2013,\n",
       " 'iPhone 6': 2014,\n",
       " 'iPhone 6S': 2015,\n",
       " 'iPhone 7': 2016,\n",
       " 'iPhone 8': 2017,\n",
       " 'iPhone X': 2017,\n",
       " 'iPhone XR': 2018,\n",
       " 'iPhone XS': 2018}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone.update({\"iPhone XR\" : 2018,\"iPhone XS\" : 2018})\n",
    "iPhone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.删除键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'iPhone 3G': 2008,\n",
       " 'iPhone 3GS': 2009,\n",
       " 'iPhone 4': 2010,\n",
       " 'iPhone 4S': 2011,\n",
       " 'iPhone 5': 2012,\n",
       " 'iPhone 5S': 2013,\n",
       " 'iPhone 6': 2014,\n",
       " 'iPhone 6S': 2015,\n",
       " 'iPhone 7': 2016,\n",
       " 'iPhone 8': 2017,\n",
       " 'iPhone X': 2017,\n",
       " 'iPhone XR': 2018,\n",
       " 'iPhone XS': 2018}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del iPhone['iPhone']\n",
    "iPhone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.修改键值对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.1 直接修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'iPhone 3G': 2008,\n",
       " 'iPhone 3GS': 2009,\n",
       " 'iPhone 4': 2010,\n",
       " 'iPhone 4S': 2011,\n",
       " 'iPhone 5': 2012,\n",
       " 'iPhone 5S': 2013,\n",
       " 'iPhone 6': 2014,\n",
       " 'iPhone 6S': 2015,\n",
       " 'iPhone 7': 2016,\n",
       " 'iPhone 8': 2017,\n",
       " 'iPhone X': 2017,\n",
       " 'iPhone XR': 2018,\n",
       " 'iPhone XS': 2019}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone[\"iPhone XS\"] = 2019\n",
    "iPhone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 函数修改update( )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'iPhone 3G': 2008,\n",
       " 'iPhone 3GS': 2009,\n",
       " 'iPhone 4': 2010,\n",
       " 'iPhone 4S': 2011,\n",
       " 'iPhone 5': 2012,\n",
       " 'iPhone 5S': 2013,\n",
       " 'iPhone 6': 2014,\n",
       " 'iPhone 6S': 2015,\n",
       " 'iPhone 7': 2016,\n",
       " 'iPhone 8': 2017,\n",
       " 'iPhone X': 2017,\n",
       " 'iPhone XR': 2019,\n",
       " 'iPhone XS': 2019,\n",
       " 'iphone X': 2019}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone.update({\"iphone X\" : 2019,\"iPhone XR\" : 2019})\n",
    "iPhone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询所有键或值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['iPhone 3G', 'iPhone 3GS', 'iPhone 4', 'iPhone 4S', 'iPhone 5', 'iPhone 5S', 'iPhone 6', 'iPhone 6S', 'iPhone 7', 'iPhone 8', 'iPhone X', 'iPhone XR', 'iPhone XS', 'iphone X'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2017, 2019, 2019, 2019])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iPhone.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三、容易犯的错误"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict['Name']:  Clancey\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'Name': 'Runoob', 'Age': 7, 'Name': 'Clancey'}\n",
    "print (\"dict['Name']: \", dict1['Name'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dict2 = {['Name']: 'Clancey', 'Age': 7}\n",
    "print (\"dict['Name']: \", dict2['Name'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 四、其他常见字典内置函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. len( )计算字典元素个数，即键的总数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3 = {'Name': 'Runoob', 'Age': 7, 'Class': 'One'}\n",
    "len(dict3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. str( )输出字典，以可打印的字符串表示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{'Name': 'Runoob', 'Age': 7, 'Class': 'One'}\""
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3 = {'Name': 'Runoob', 'Age': 7, 'Class': 'One'}\n",
    "str(dict3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.type( )返回输入的变量类型，如果变量是字典就返回字典类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3 = {'Name': 'Runoob', 'Age': 7, 'Class': 'One'}\n",
    "type(dict3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
